package cn.tedu.mall.seckill.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.pojo.seckill.dto.SeckillOrderItemAddDTO;
import cn.tedu.mall.pojo.seckill.vo.SeckillSkuVO;
import cn.tedu.mall.seckill.service.ISeckillSkuService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
public class SeckillSkuServiceImpl implements ISeckillSkuService {
    @Autowired
    private RedisTemplate redisTemplate;

    public static final String SECKILL_SKUS_PREFIX = "seckill:skus:";
    public static final String SECKILL_SKU_STOCK_PREFIX = "seckill:sku:stock:";

    @Override
    public List<SeckillSkuVO> listSeckillSkus(Long spuId) {
        List<SeckillSkuVO> seckillSkuVOS =
                (List<SeckillSkuVO>) redisTemplate.
                        opsForValue().get(SECKILL_SKUS_PREFIX + spuId);
        if (seckillSkuVOS == null || seckillSkuVOS.size() == 0) {
            throw new CoolSharkServiceException(
                    ResponseCode.NOT_FOUND, "秒杀sku列表查询为空,请检查预热数据");
        }
        return seckillSkuVOS;
    }

    @Override
    public void redusSeckillSkuCount(SeckillOrderItemAddDTO dto, String sn) {

    }

    @Override
    public void returnStock(String sn) {

    }
}
